package ru.spbu.math.android.cbrf.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.model.Category;
import ru.spbu.math.android.cbrf.model.RateType;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class CategoryListAdapter implements ListAdapter {
	
	private List<Category> categories;
	private Category selectedCategory;
	private RateType selectedRateType;

	private Activity activity;
	private int appWidgetId;
	private Map<Category, View> views;

	/**
	 * @return current selected Category
	 */
	public Category getSelectedCategory() {
		return selectedCategory;
	}

	/**
	 * set selected Category and mark previous selected Category as inactive 
	 * @param selectedCategory
	 */
	public void setSelectedCategory(Category selectedCategory) {
		if ((this.selectedCategory != null)
				&& (!selectedCategory.equals(this.selectedCategory))) {
			deselectCategory();
		}
		this.selectedCategory = selectedCategory;
	}

	/**
	 * set view related to selectedCategory selected
	 */
	private void selectCategory() {

		if (selectedCategory != null && selectedRateType != null) {
			View view = views.get(selectedCategory);
			ImageView imView = ((ImageView) view.findViewById(R.id.item_icon));
			imView.setImageResource(R.drawable.list_item_icon_active);

			((TextView) view.findViewById(R.id.bottomtext))
					.setText(selectedRateType.getName());
		}
	}

	/**
	 * set view related to selectedCategory not selected
	 */
	private void deselectCategory() {
		if (selectedCategory != null) {
			View view = views.get(selectedCategory);
			ImageView imView = ((ImageView) view.findViewById(R.id.item_icon));
			imView.setImageResource(R.drawable.list_item_icon_inactive);

			((TextView) view.findViewById(R.id.bottomtext)).setText("");
		}
	}
	
	public CategoryListAdapter(List<Category> categories, Activity activity) {
		this.categories = categories;
		this.activity = activity;

		views = new HashMap<Category, View>();
		for (Category category : categories) {
			views.put(category, makeViewFromCategory(category));
		}
	}

	@Override
	public int getCount() {
		return categories.size();
	}

	@Override
	public Object getItem(int position) {
		return categories.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return ListAdapter.IGNORE_ITEM_VIEW_TYPE;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return views.get(categories.get(position));
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isEmpty() {
		return categories.isEmpty();
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public boolean isEnabled(int position) {
		return true;
	}

	/**
	 * @param category
	 * @return make view that represent category
	 */
	private View makeViewFromCategory(Category category) {
		View res = LayoutInflater.from(activity.getApplicationContext()).inflate(
				R.layout.category_list_item, null);

		ImageView imView = ((ImageView) res.findViewById(R.id.item_icon));
		if (selectedCategory != null && selectedCategory.equals(category)) {
			imView.setImageResource(R.drawable.list_item_icon_active);
		} else {
			imView.setImageResource(R.drawable.list_item_icon_inactive);
		}

		((TextView) res.findViewById(R.id.toptext)).setText(category.getName());
		((TextView) res.findViewById(R.id.bottomtext)).setText("");

		return res;
	}

	/**
	 * set selectedRate type and set corresponding category view selected
	 * 
	 * @param selectedRateType
	 */
	public void setSelectedRateType(RateType selectedRateType) {
		this.selectedRateType = selectedRateType;
		
		Intent resultValue = new Intent();
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		activity.setResult(Activity.RESULT_OK, resultValue);
		
		selectCategory();
	}

	/**
	 * @return current selected RateType
	 */
	public RateType getSelectedRateType() {
		return selectedRateType;
	}
}
